
#include "file_list.h"



int strcmp_c(const char *s1, const char *s2)
{
	int i;
	char c1, c2;

	for (i = 0, c1 = *s1, c2 = *s2; (c1 != '\0') && (c2 != '\0'); i++)
	{
		c1 = *(s1 + i);
		c2 = *(s2 + i);
		if ((c1 >= 'A') && (c1 <='Z')) c1 += 'a' - 'A';
		if ((c2 >= 'A') && (c2 <='Z')) c2 += 'a' - 'A';
		if (c1 != c2) break;
	}

	return (int)c1 - (int)c2;
}


void file_get_fullname(char *fullname,char *path, char *name)
{
	strcpy(fullname, path);
	strcat(fullname, "/");
	strcat(fullname, name);
}


int file_list_trim(FileListHandle handle, char* dirpath)
{
	int					ret;
	pFileList						pfile_list;
	
	
	pfile_list = (pFileList) handle;
	if ((!dirpath) || (!pfile_list))
	{
		return RET_FAILURE;
	}
	
	pfile_list->dirpath = malloc(strlen((char *)dirpath)+1);
	if (pfile_list->dirpath ==NULL)
	{
		log_n("malloc failed!\n");
		return RET_FAILURE;
	}
	
	strcpy(pfile_list->dirpath, (char *)dirpath);
	
	pfile_list->sortorder = 0;
	pfile_list->sorttype = 0;

	ret = file_list_sort(pfile_list, SORT_NAME);
	if (ret != RET_SUCCESS)
	{
		log_n("sort_file failed!\n");
		return ret;
	}
	
	
	return RET_SUCCESS;
		
}


void file_list_clean(pFileList pfile_list)
{
	struct list_head			*ptr = NULL, *ptn = NULL;
	pFileListItem				item = NULL;

	
	if (!pfile_list)
	{
		log_n("Invalid Parameters!\n");
		return;
	}
	
	if(!list_empty(&pfile_list->head))
	{
		list_for_each_safe(ptr, ptn, &pfile_list->head)
		{
			item = list_entry(ptr, FileListItem, listpointer);
			list_del(ptr);
			free(item);
		}
	}
	
	list_del(&pfile_list->listpointer);

	free(pfile_list->dirpath);
	pfile_list->dirpath = NULL;
	
	pfile_list->filenum = 0;
	pfile_list->dirnum = 0;
	
	INIT_LIST_HEAD(&pfile_list->head);
	INIT_LIST_HEAD(&pfile_list->listpointer);
		
	return;

}

void file_list_print(pFileList pfile_list)
{
	struct list_head						*ptr = NULL;
	pFileListItem							item = NULL;
	uint16_t								i=0;


	list_for_each(ptr, &pfile_list->head)
	{
		item = list_entry(ptr, FileListItem, listpointer);
		if(item != NULL)
		{
			if(item->type == F_DIR)
			{
				log_n("FL [DIR] %d\t %s\n", i++, item->name);
			}
			else
			{
				log_n("FL [FILE] %d\t %s\n", i++, item->name);
			}
		}
	}


	return;
}


pFileListItem	get_file_list_item_by_index(pFileList pfile_list, unsigned short index)
{
	pFileListItem							item = NULL;
	struct list_head						*ptr;

	if ((!pfile_list) || ((index<=0) || (index > (pfile_list->dirnum+pfile_list->filenum))))
	{
		log_n("Invalid Parameters!\n");
		return item;
	}
	
	if((index*2) > (pfile_list->dirnum+pfile_list->filenum))
	{
		index = pfile_list->dirnum+pfile_list->filenum+1-index;
		list_for_each_prev(ptr, &pfile_list->head)
		{
			if(!--index)
			{
				item = list_entry(ptr, FileListItem, listpointer);
				break;
			}
		}
	}
	else
	{
		list_for_each(ptr, &pfile_list->head)
		{
			if(!--index)
			{
				item = list_entry(ptr, FileListItem, listpointer);
				break;
			}
		}
	}

	
	return item;
}


int compare_filelist_items(struct list_head *p1, struct list_head *p2, struct CompareParameter *p_para)
{
	char												*c1, *c2;
	char												*p_ctemp;
	char												t1,t2;
	int												temp1, temp2;
	unsigned int										s1,s2;
	pFileListCompareContext							compare_para;


	compare_para = (pFileListCompareContext)p_para->context;
	
	if (compare_para->id_check_playlist == 0)
	{
		switch(compare_para->mode)
		{
			#if 0
			case SORT_FAV:
			{
				t1 = list_entry(p1, FileListItem, listpointer)->in_play_list;
				t2 = list_entry(p2, FileListItem, listpointer)->in_play_list;
				if (t1 != t2)
				{
					return (int)(t2 - t1); 
				}
			}
			#endif
			
			case SORT_NAME:
			{
				c1 = list_entry(p1, FileListItem, listpointer)->name;
				c2 = list_entry(p2, FileListItem, listpointer)->name;
				
				while ((*c1 != '\0') && (*c2 != '\0'))
				{
					if (*c1 != *c2) 
					{
						break;
					}
					c1++;
					c2++;
				}
				return (int)((unsigned char)(*c1)) - (int)((unsigned char)(*c2));
			}
/*
			case SORT_NAME_Z_A:
			{
				c1 = list_entry(p1, FileListItem, listpointer)->name;
				c2 = list_entry(p2, FileListItem, listpointer)->name;
				
				while ((*c1 != '\0') && (*c2 != '\0'))
				{
					if (*c1 != *c2) 
					{
						break;
					}
					c1++;
					c2++;
				}
				return (int)((unsigned char)(*c2)) - (int)((unsigned char)(*c1));
			}
*/
			case SORT_DATE:
			{
				s1 = list_entry(p1, FileListItem, listpointer)->time;
				s2 = list_entry(p2, FileListItem, listpointer)->time;

				return (int)(s1-s2);
			}

			case SORT_SIZE:
			{
				s1 = list_entry(p1, FileListItem, listpointer)->size;
				s2 = list_entry(p2, FileListItem, listpointer)->size;

                if(s1>s2)
                {
                    return 1;
                }
                else if(s1<s2)
                {
                    return -1;
                }
                else
                {
                    return 0;
                }
				//return (int)(s1-s2); 
			}
			
			default:
			{
				return 0;
			}
		}
	}

	return 0;
}


FileListHandle file_list_create(FileListType type)
{
	pFileList						pfile_list = NULL;


	if ((type>=MEDIA_PLAYER_FILE_LIST_NUMBER) /*|| (type<MUSIC_FILE_LIST)*/)
	{
		log_n("Invalid Parameters!\n");
		return NULL;
	}

	pfile_list = (pFileList) malloc(sizeof(FileList));
	if (!pfile_list)
	{
		log_n("malloc failed!\n");
		return NULL;
	}

	memset(pfile_list, 0, sizeof(FileList));

	INIT_LIST_HEAD(&pfile_list->head);
	INIT_LIST_HEAD(&pfile_list->listpointer);

	pfile_list->type = type;
		
	return (FileListHandle)pfile_list;
		
}

void file_list_delete(FileListHandle handle)
{
	struct list_head			*ptr = NULL, *ptn = NULL;
	pFileListItem				item = NULL;
	pFileList					pfile_list;
	

	pfile_list = (pFileList) handle;
	if (!pfile_list)
	{
		log_n("Invalid Parameters!\n");
		return;
	}
	
	if(!list_empty(&pfile_list->head))
	{
		list_for_each_safe(ptr, ptn, &pfile_list->head)
		{
			item = list_entry(ptr, FileListItem, listpointer);
			list_del(ptr);
			free(item);
		}
	}
	
	list_del(&pfile_list->listpointer);
	free(pfile_list->dirpath);
	
	free(pfile_list);
		
	return;
}


FileType file_map_type(FileListType list_type, char *ext_name)
{
    FileType file_type = F_UNKOWN;
    
    uint32_t  music_type 	     = F_MP3*F_BYE1*F_OGG*F_WAV*F_FLC*F_PCM;
    uint32_t  video_type 	     = F_MPG*F_MPEG;
    uint32_t  image_type 		 = F_JPG*F_BMP;
	uint32_t	video_photo_type = F_MPG*F_MPEG*F_JPG*F_BMP;
	uint32_t	video_photo_music_type = F_MPG*F_MPEG*F_JPG*F_BMP*F_MP3*F_PCM;;


	if(!strcmp_c(ext_name, "MP3"))
	{
		file_type = F_MP3;
	}
	else if(!strcmp_c(ext_name, "MP2"))
	{
		file_type = F_MP3;
	}
	else if(!strcmp_c(ext_name, "MP1"))
	{
		file_type = F_MP3;
	}
	else if(!strcmp_c(ext_name, "MPA"))
	{
		file_type = F_MP3;
	}
	else if(!strcmp_c(ext_name, "OGG"))
	{
		file_type = F_OGG;
	}
    else if(!strcmp_c(ext_name, "AAC"))
	{
		file_type = F_MP3;
	}
    else if(!strcmp_c(ext_name, "AC3"))
	{
		file_type = F_MP3;
	}
	else if(!strcmp_c(ext_name, "PCM"))
	{
		file_type = F_MP3;
	}
	#if 0
	else if(strcmp_byex(ext_name, "BYE1"))
	{
		file_type = F_BYE1;
	}
	#endif
    else if(!strcmp_c(ext_name, "WAV"))          
	{
		file_type = F_WAV;
	}
    else if(!strcmp_c(ext_name, "FLAC"))         //support the flac output
	{
		file_type = F_FLC;                                 //FLAC icon shows the "FLC" by Ivan 2012/9/19
	}
	else if(!strcmp_c(ext_name, "JPG"))
	{
		file_type = F_JPG;
	}   
    else if(!strcmp_c(ext_name, "JPEG"))
	{
		file_type = F_JPG;
	}    
	else if(!strcmp_c(ext_name, "HEIC"))
	{
		file_type = F_JPG;
	}
	else if(!strcmp_c(ext_name, "BMP"))
	{
		file_type = F_BMP;
	}
	else if(!strcmp_c(ext_name, "GIF"))
	{
		file_type = F_BMP;
	}
	else if(!strcmp_c(ext_name, "PNG"))
	{
		file_type = F_BMP;
	}
	else if(!strcmp_c(ext_name, "MPG"))
	{
		 file_type = F_MPG;
	}
	else if(!strcmp_c(ext_name, "MPEG"))
	{
		file_type = F_MPEG;
	}	
	else if(!strcmp_c(ext_name, "QLV"))
	{
		file_type = F_MPEG;
	}
    else if(!strcmp_c(ext_name, "DAT"))
	{
		file_type = F_MPEG;
	}
	else if(!strcmp_c(ext_name, "VOB"))
	{
		file_type = F_MPEG;
	}
	else if(!strcmp_c(ext_name, "AVI"))
	{
	    file_type = F_MPG;
	}
    else if(!strcmp_c(ext_name, "TS"))
    {
        file_type = F_MPG;
    }
    else if(!strcmp_c(ext_name, "TRP"))
    {
        file_type = F_MPG;
    }
    else if(!strcmp_c(ext_name, "TP"))
    {
        file_type = F_MPG;
    }
    else if(!strcmp_c(ext_name, "M2T"))
    {
        file_type = F_MPG;
    }
    else if(!strcmp_c(ext_name, "M2TS"))
    {
        file_type = F_MPG;
    }
    else if(!strcmp_c(ext_name, "MTS"))
    {
        file_type = F_MPG;
    }
    else if(!strcmp_c(ext_name, "MP4"))
    {
        file_type = F_MPG;
    }
    else if(!strcmp_c(ext_name, "MKV"))
    {
        file_type = F_MPG;
    }
    else if(!strcmp_c(ext_name, "MOV"))
    {
        file_type = F_MPG;
    }
    else if(!strcmp_c(ext_name, "3GP"))
    {
        file_type = F_MPG;
    }
	#if 0
    else if(strcmp_byex(ext_name, "BYE2"))
    {
        file_type = F_MPG;
    }
    else if(strcmp_byex(ext_name, "BYE3"))
    {
        file_type = F_MPG;
    }
	#endif
	else if(!strcmp_c(ext_name, "FLV"))
    {
        file_type = F_MPG;
    }
	else if(!strcmp_c(ext_name, "RMVB"))
	{
		file_type = F_MPG;
	}
	else if(!strcmp_c(ext_name, "RM"))
	{
		file_type = F_MPG;
	}
	else if(!strcmp_c(ext_name, "RAM"))
	{
		file_type = F_MPG;
	}
	else if(!strcmp_c(ext_name, "WEBM"))
	{
		file_type = F_MPG;
	}
	else if(!strcmp_c(ext_name, "ES"))	
	{
		file_type = F_MPG;
	}				
    else
    {
        file_type = F_UNKOWN;
    }


    if (list_type == MUSIC_FILE_LIST)
    {
        if (music_type % file_type != 0)
            file_type = F_UNKOWN;
    }
    else if (list_type == IMAGE_FILE_LIST)
    {
        if (image_type % file_type != 0)
            file_type = F_UNKOWN;
    }
    else if (list_type == VIDEO_FILE_LIST)
    {
        if (video_type % file_type != 0)
            file_type = F_UNKOWN;
    }
	else if (list_type == VIDEO_PHOTO_FILE_LIST)
    {
        if (video_photo_type % file_type != 0)
            file_type = F_UNKOWN;
    }
	else if (list_type == VIDEO_PHOTO_MUSIC_FILE_LIST)
    {
        if (video_photo_music_type % file_type != 0)
            file_type = F_UNKOWN;
    }
    else if (list_type == DLNA_FILE_LIST)
    {
    
    }
    else 
        file_type = F_UNKOWN;


    return file_type;
        
}


int file_list_read_folder(FileListHandle handle, char *dirpath)
{
	FileType						file_type;
	FileListType					type;
	struct dirent					*items = NULL;
	pFileListItem					pfile_list_item = NULL;
	unsigned int					start_time, end_time;
	unsigned int					entry_cnt = 0;
	pFileList						pfile_list;
	
	DIR *dirfd 						= NULL;
	struct dirent					*entry;
	char							buff[sizeof(struct dirent) + MAX_FILE_NAME_SIZE];
	struct stat					file_attribute;
	int							ret;
	char							full_path[FULL_PATH_SIZE];
	int							dirpath_length;


	pfile_list = (pFileList) handle;
	if ((!dirpath) || (!pfile_list))
	{
		log_n("Invalid Parameters!\n");
		return RET_FAILURE;
	}

	type = pfile_list->type;

	memset(full_path, 0, FULL_PATH_SIZE);
	strcpy(full_path, dirpath);
	strcat(full_path, "/");
	dirpath_length = strlen(dirpath) + 1;

	dirfd = opendir(dirpath);
	if(!dirfd)
	{
		log_n("<<====read_folder_to_file_list()\n");
		return RET_FAILURE;
	}
	
	entry = (struct dirent *)buff;
	
	file_list_clean(pfile_list);
	
	
	while (1)
	{
		entry = readdir(dirfd);
		if (!entry)
			break;

		
		if (S_ISDIR(entry->d_type)) //dir
		{
			if (!strcmp(entry->d_name, "."))
			{
				continue;
			}
			
			if (!strcmp(entry->d_name, ".."))
			{
				continue;
			}
			
			if (!strcmp_c(entry->d_name, "ALiDvr"))
			{
				continue;
			}
			if (!strcmp_c(entry->d_name, "ALIDVRS2"))
			{
				continue;
			}			
			/*
			if (!strcmp_c(entry->d_name, udisk_filter))
			{
				continue;
			}
			*/
			if (!strcmp_c(entry->d_name, "recycler"))
			{
				continue;
			}
			if (!strcmp_c(entry->d_name, "$RECYCLE.BIN"))
			{
				continue;
			}
			if (!strcmp_c(entry->d_name, "system volume information"))
			{
				continue;
			}
			
			if (!strcmp_c(entry->d_name, "System Volume Information"))
			{
				continue;
			}	
			
			pfile_list_item = (pFileListItem)malloc(sizeof(FileListItem) + strlen(entry->d_name) + 1);
			if (pfile_list_item == NULL)
			{
				break;
			}
			
			memset(pfile_list_item, 0, sizeof(FileListItem));

			pfile_list_item->type = F_DIR;
			
			strcpy(pfile_list_item->name, entry->d_name);

			pfile_list->dirnum++;

			list_add(&pfile_list_item->listpointer, &pfile_list->head);


		}
		else //file
		{
			int j, k;
			for (j = 0, k = -1; entry->d_name[j] != '\0'; j++)
			{
				if (entry->d_name[j] == '.') k = j;
			}
			if (k == -1)
			{
				continue;
			}
			else
			{
				k++;
				
				file_type = F_UNKOWN;

				//debug_i("entry->d_name=%s\n",entry->d_name);
				//debug_i("type=%d\n",type);

                file_type = file_map_type(type, entry->d_name + k);
				if (file_type == F_UNKOWN)
				{
					continue;
				}
				
				//debug_i("file_type=%d\n",file_type);

				pfile_list_item = (pFileListItem)malloc(sizeof(FileListItem) + strlen(entry->d_name) + 1);
				if (pfile_list_item == NULL)
				{
					break;
				}
				
				memset(pfile_list_item, 0, sizeof(FileListItem));

				pfile_list_item->type = file_type;

				strcpy((full_path + dirpath_length), entry->d_name);

				#if 0
				ret = fs_stat(full_path, &file_attribute);
				if (ret >= 0)
				{
					pfile_list_item->size = file_attribute.st_size;
					pfile_list_item->time = file_attribute.st_ctime;
				}
				#endif

				strcpy(pfile_list_item->dirpath,dirpath);
				strcpy(pfile_list_item->name, entry->d_name);

				pfile_list->filenum++;

				list_add_tail(&pfile_list_item->listpointer, &pfile_list->head);
			}
		}
		
		
		entry_cnt++;

		if (entry_cnt >= MAX_FILE_NUMBER)
		{
			break;
		}
		
	}

	closedir(dirfd);
	
	if(file_list_trim(pfile_list, dirpath) == RET_SUCCESS)
	{
		log_n("trim succeed.\n");

	}
	
	
	if(pfile_list->filenum == 0)
	{
		return RET_FAILURE;
	}
	
	return RET_SUCCESS;
}


int file_list_read_all_file(FileListHandle handle, char *dirpath,int check)
{
	FileType						file_type;
	FileListType					type;
	struct dirent					*items = NULL;
	pFileListItem					pfile_list_item = NULL;
	unsigned int					start_time, end_time;
	int								entry_cnt = 0;
	pFileList						pfile_list;
	DIR *dirfd = NULL;
	struct dirent					*entry;
	struct dirent					*entry_T;
	char							buff[sizeof(struct dirent) + MAX_FILE_NAME_SIZE];
	char							buff_T[sizeof(struct dirent) + MAX_FILE_NAME_SIZE];
	struct stat					    file_attribute;
	int							    ret,t;
	char							full_path[FULL_PATH_SIZE];
	int							    dirpath_length;
	char dirpath2[128]={0};
	char name_tmp[128]={0};


	pfile_list = (pFileList) handle;
	if ((!dirpath) || (!pfile_list))
	{
		log_n("Invalid Parameters!\n");
		return RET_FAILURE;
	}

	type = pfile_list->type;

	memset(full_path, 0, FULL_PATH_SIZE);
	strcpy(full_path, dirpath);

	strcat(full_path, "/");
	dirpath_length = strlen(dirpath) + 1;

	
	dirfd = opendir(dirpath);
	if(!dirfd)
	{
		log_n("open dir error\n");
		return RET_FAILURE;
	}
	
	entry = (struct dirent *)buff;
	entry_T = (struct dirent *)buff_T;
	
	if(!check)
	{
		file_list_clean(pfile_list);
		//check = 1;
	}
	
	while (1)
	{

		entry_T = readdir(dirfd);
		if (!entry_T)
			break;


		if(entry_T->d_type == 4) //dir
		{
			//debug_i("dir\n");

			if (!strcmp(entry_T->d_name, "."))
			{
				continue;
			}
			
			if (!strcmp(entry_T->d_name, ".."))
			{
				continue;
			}
			
			if (!strcmp_c(entry_T->d_name, "ALiDvr"))
			{
				continue;
			}
			if (!strcmp_c(entry_T->d_name, "ALIDVRS2"))
			{
				continue;
			}			
			/*
			if (!strcmp_c(entry->d_name, udisk_filter))
			{
				continue;
			}
			*/
			if (!strcmp_c(entry_T->d_name, "recycler"))
			{
				continue;
			}
			
			if (!strcmp_c(entry_T->d_name, "system volume information"))
			{
				continue;
			}
			
			if (!strcmp_c(entry_T->d_name, "System Volume Information"))
			{
				continue;
			}	
			
			pfile_list_item = (pFileListItem)malloc(sizeof(FileListItem) + strlen(entry_T->d_name) + 1);
			if (pfile_list_item == NULL)
			{
				break;
			}
			
			strcpy(dirpath2,dirpath);
			strcat(dirpath2,"/");
			strcat(dirpath2,entry_T->d_name);
			

			file_list_read_all_file(handle, dirpath2,1);
		}
		else //file
		{
			
			int j, k;
			for (j = 0, k = -1; entry_T->d_name[j] != '\0'; j++)
			{
				if (entry_T->d_name[j] == '.') k = j;
			}

			if (k == -1)
			{
				continue;
			}
			
			else
			{
				k++;
				
				file_type = F_UNKOWN;

                file_type = file_map_type(type, entry_T->d_name + k);
				
				if (file_type == F_UNKOWN)
				{
					continue;
				}

				pfile_list_item = (pFileListItem)malloc(sizeof(FileListItem) + strlen(entry_T->d_name) + 1);
				if (pfile_list_item == NULL)
				{
					break;
				}
				
				memset(pfile_list_item, 0, sizeof(FileListItem));

				pfile_list_item->type = file_type;
				

				strcpy(pfile_list_item->dirpath,dirpath);
				strcpy(pfile_list_item->name, entry_T->d_name);

				//log_i("name = %s\n",entry_T->d_name);

				pfile_list->filenum++;
				entry_cnt++;
				list_add_tail(&pfile_list_item->listpointer, &pfile_list->head);
			}
		}

		if (entry_cnt >= MAX_FILE_NUMBER)
		{
			break;
		}
		
	}
	
	closedir(dirfd);

	if(file_list_trim(pfile_list, dirpath) == RET_SUCCESS)
	{
		log_n("trim succeed!\n");	
	}
	
	if(pfile_list->filenum == 0)
	{
		return RET_FAILURE;
	}
	
	return RET_SUCCESS;
}




int file_list_sort(FileListHandle handle, FileListSortType sort_type)
{
	pFileListItem								item;
	struct CompareParameter					para;
	FileListCompareContext						compare_para;
	uint16_t									i;
	uint32_t									start_time, end_time;
	struct list_head 							*left=NULL, *right=NULL;
	pFileList									pfile_list;
	
	
	pfile_list = (pFileList) handle;
	if ((!pfile_list) || (/*(sort_type<SORT_RAND) ||*/(sort_type>SORT_FAV)))
	{
		log_n("Invalid Parameters!\n");
		return RET_FAILURE;
	}
		pfile_list->sorttype = sort_type;
		pfile_list->sortorder = 0;
		

		compare_para.id_check_playlist = 0;
		compare_para.mode = pfile_list->sorttype;
		para.context = &compare_para;
		para.compare = compare_filelist_items;
		if(pfile_list->dirnum > 1)
		{
			//sort dir
			left = pfile_list->head.next;
			item = get_file_list_item_by_index(pfile_list, pfile_list->dirnum);
			if(item == NULL)
			{
				return RET_FAILURE;
			}
			right = &item->listpointer;
			if (quick_sort_list(left, right, &para) != 0)
			{
				;
			}
		}
		
		if(pfile_list->filenum > 1)
		{
			//sort file
			item = get_file_list_item_by_index(pfile_list, pfile_list->dirnum+1);
			if(item == NULL)
			{
				return RET_FAILURE;
			}
			left = &item->listpointer;
			right = pfile_list->head.prev;
			if (quick_sort_list(left, right, &para) != 0)
			{
				;
			}
		}


		
	return RET_SUCCESS;
}

int file_list_get_info(FileListHandle handle, unsigned int *dir_number, unsigned int *file_number, char* current_path)
{
	pFileList								pfile_list;
	

	pfile_list = (pFileList) handle;
	if (!pfile_list)
	{
		log_n("Invalid Parameters!\n");
		return RET_FAILURE;
	}

	if (dir_number)
	{
		*dir_number = pfile_list->dirnum;
	}

	if (file_number)
	{
		*file_number = pfile_list->filenum;
	}

	if (current_path)
	{
		strcpy(current_path, pfile_list->dirpath);
	}
	
	
	return RET_SUCCESS;

}


int file_list_get_file(FileListHandle handle, unsigned short index, pFileInfo pinfo)
{
	pFileListItem						item;
	pFileList							pfile_list;
	
	
	pfile_list = (pFileList) handle;
	if ((!pinfo)  || (!pfile_list))
	{
		log_n("Invalid Parameters!\n");
		return RET_FAILURE;
	}

	
	if (index > (pfile_list->dirnum + pfile_list->filenum))
	{
		log_n("Invalid Index!\n");
		return RET_FAILURE;
	}
		
	
	item = get_file_list_item_by_index(pfile_list, index);
	if(item == NULL)
	{
		log_n("<<====file_list_get_file()\n");
		return RET_FAILURE;
	}
		
	pinfo->filetype     = item->type;
	pinfo->in_play_list = item->in_play_list;
	pinfo->size         = item->size;
	
	strcpy(pinfo->path, item->dirpath);
	strcpy(pinfo->name, item->name);
	

	
	return RET_SUCCESS;
}



